home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 January: Mac OS SDK / Dev.CD Jan 99 SDK1.toast / Development Kits / AppleScript / Development Tools / Tools Goodies / AEGizmos 1.4.1 / Documentation / AE Builder⁄Printer Doc.dp (.txt) < prev    next >
Encoding:
Common Ground  |  1995-03-20  |  60.3 KB  |  955 lines  |  [CGDC/CGVM]

  1. Palatino
  2. Apple Events
  3.  Builder/Printer
  4. AEGizmos Version 1.4
  5. Jens Peter Alfke
  6. 20 March 1995
  7.  Apple Computer, Inc. 1991
  8. New York
  9. Palatino
  10. The Apple Event Builder/Printer
  11. 20 March 1995
  12. Page 
  13. Contents
  14. Contents
  15. )4 ................................
  16. )` ................................
  17. )` ................................
  18. ..............
  19. Introduction
  20. )F ................................
  21. )` ................................
  22. )` ................................
  23. ..........
  24. OK, What Is It?
  25. s New?
  26. How To Call the Functions
  27.  ................................
  28. )` ................................
  29. ................
  30. AEBuild
  31. AEBuildParameters
  32. AEBuildAppleEvent
  33. AEPrint
  34. Descriptor-String Syntax
  35.  ................................
  36. )` ................................
  37. ...................
  38. Basic Types
  39. Coercion
  40. Lists
  41. Records
  42. Apple Events
  43. Substituting Parameters
  44. Descriptor-String Grammar
  45.  ................................
  46. )` ................................
  47. ............
  48. An Example & Timing Comparison
  49.  ................................
  50. ..............................
  51. #C Code Using Object-Packing Library
  52. Descriptor String
  53. AEBuild Call
  54. Timing Conclusions
  55. The Demo Program
  56. )m ................................
  57. )` ................................
  58. ...........................
  59. The Header Files
  60. )^ ................................
  61. )` ................................
  62. )` ................................
  63.     AEBuild.h
  64. AEBuildGlobals.h
  65.     AEPrint.h
  66. New York
  67. Palatino
  68. The AppleEvent Builder/Printer
  69. 20 March 1995
  70. Page 
  71. Introduction
  72. OK, What Is It?
  73. KEven with the helpful routines in AEPackObject.h that assemble common Apple
  74. Oevent object descriptors, building descriptors and events is still a pain. I
  75. Lwritten a library of a few functions that make it quick and easy to build or
  76. @display Apple event descriptors and the Apple events themselves.
  77. Courier
  78. AEBuild
  79. )*@ function takes a format string 
  80.  a description in a very simple
  81. fNlanguage of an Apple event descriptor 
  82.  and generates a real descriptor (which
  83. 3could be a record or list or event) out of it. The 
  84. AEPrint
  85.  function does the reverse:
  86. fUgiven an Apple event descriptor, list or record, it prettyprints it to a string. (The
  87. resulting string, if sent to 
  88. AEBuild
  89. , would reproduce the original 
  90. AEDesc
  91. structure.)
  92. AEBuild
  93. )*H can plug variable parameters into the structures it generates 
  94.  as with
  95. printf
  96. )$I, all you do is put marker characters in the format string and supply the
  97. f-parameter values as extra function arguments.
  98. 0The benefits of using this library are fourfold:
  99. Zapf Dingbats
  100. s easier for you to write the code to build Apple event structures. You
  101. Fonly have to remember one function call and a few simple syntax rules.
  102. 1Your resulting code is also easier to understand.
  103. -As of version 1.2, your code is even faster: 
  104. AEBuild
  105.  is three to four times
  106. Cas fast as the regular Apple Event Manager routines at constructing
  107. ,complex structures. (Your mileage may vary.)
  108. #Your code is smaller: the code for 
  109. AEBuild
  110.  and the AEStream library is
  111. Iabout 6k in size, and the overhead for each call is minimal. (Most of the
  112. Jdescriptor string consists of the same four-letter codes you
  113. d be using in
  114. ?your program code anyway, and the strings can even be stored in
  115. !resources for more code savings.)
  116. AEPrint
  117. )*4 helps in debugging programs, by turning mysterious 
  118. AEDesc
  119. $structures into human-readable text.
  120. Palatino
  121. The Apple Event Builder/Printer
  122. Page 
  123. 20 March 1995
  124.  What
  125. s New?
  126. In version 1.3.2:
  127. Courier
  128. AEPrint
  129.  no longer uses the 
  130. stdio
  131. + library. This should help reduce code size
  132. ^0(and eliminate some problems in code resources).
  133. And in version 1.3.3:
  134. LAfter a brilliant suggestion by Rob Dye, AEPrint now uses the built in float
  135. ^7to-text coercion to display floating-point descriptors.
  136. EFixed a possible problem with AEBuild input strings containing Return
  137. .characters, in the MPW version of the library.
  138. And in version 1.4:
  139. NFixed an AEPrint bug that was changing the type of list descriptors to 
  140. LFixed problems parsing non-ascii characters (notably 
  141. ) in the CodeWarrior
  142. version of the library.
  143.  descriptors are built properly (one byte, not two.)
  144. JFixed AEPrint quoting problems on 4-char codes that could cause the output
  145. to be unparseable by AEBuild.
  146. Palatino
  147. The Apple Event Builder/Printer
  148. 20 March 1995
  149. Page 
  150. How To Call the Functions
  151. fRThese are all C functions. They all take variable number s of arguments, so they
  152. Tbe difficult to call from Pascal, anyway. (People keep telling me it
  153. s possible, but
  154. Tno one has ever sent me a Pascal header for these functions; if anyone does so, I
  155. include it in future versions.)
  156. AEBuild
  157. Courier
  158. OSErr
  159. 9AEBuild(  AEDesc *desc, const char *descriptorStr, ... ),
  160. @vAEBuild( AEDesc *desc, const char *descriptorStr, void *args );
  161. AEBuild
  162. )*K reads a null-terminated descriptor string (usually a constant, although it
  163. f@could come from anywhere), parses it and builds a corresponding 
  164. AEDesc
  165. fQstructure. (Don
  166. t worry, I
  167. ll describe the syntax of the descriptor string in the
  168. Mnext section.) If the descriptor string contains magic parameter-substitution
  169. characters (
  170. ) then corresponding values of the correct type must be supplied
  171. f$as function arguments, just as with 
  172. printf
  173. vAEBuild
  174.  is analogous to 
  175. vprintf
  176. )*.: Instead of passing the parameters along with
  177. the function, you supply a 
  178. va_list
  179. , as defined in 
  180. <stdarg.h>
  181. , that points to the
  182. f*parameter list. It
  183. s otherwise identical.)
  184. AEBuild
  185.  returns an 
  186. OSErr
  187. 5. Any errors returned by Apple Event Manager routines
  188. fSwhile building the descriptor will be sent back to you. The most likely results are
  189. memFullErr
  190.  and 
  191. errAECoercionFail
  192. . Also likely is 
  193. aeBuildSyntaxErr
  194. , resulting
  195. fQfrom an incorrect descriptor string. (Make sure to debug your descriptor strings,
  196. Eperhaps using the demo application, before you put them in programs!)
  197. The basic version of 
  198. AEBuild
  199. )*3 just reports that a syntax error occurred, without
  200. fOgiving any additional information. If you want to know more (perhaps the string
  201. Ocame from a user, to whom you
  202. d like to report a helpful error message) you can
  203. Tuse the other version of the library. This version includes a wee bit of extra code,
  204. Sand two global variables that will contain useful information after a syntax error:
  205. Palatino
  206. The Apple Event Builder/Printer
  207. Page 
  208. 20 March 1995
  209. Courier
  210. extern AEBuild_SyntaxErrType
  211. AEBuild_ErrCode;
  212. extern long
  213. AEBuild_ErrPos;
  214. AEBuild_ErrCode
  215. )Z@ is an enumerated value that will contain a specific error code.
  216. N*The error codes are defined in AEBuild.h. 
  217. AEBuild_ErrPos
  218.  will contain the index
  219. NRinto the descriptor string at which the error occurred: usually one character past
  220. the end of the offending token.
  221. AEBuildParameters
  222. OSErr
  223. )$HAEBuildParameters(  AppleEvent *event, const char *descriptorStr, ... );
  224. AEBuildParameters
  225. )f7 adds parameters and/or attributes to an existing Apple
  226. event. 
  227. descriptorStr
  228. )N5 specifies the parameters (required and optional) and
  229. NIattributes. Its syntax is described below (see especially the Apple Event
  230. GDescriptor Strings subsection); it
  231. s almost the same as the syntax for 
  232. AEBuild
  233. N'with a few additions and modifications.
  234. vAEBuildParameters
  235.  is analogous to 
  236. vprintf
  237. )*#: Instead of passing the parameters
  238. N&along with the function, you supply a 
  239. va_list
  240. , as defined in 
  241. <stdarg.h>
  242. , that
  243. N8points to the parameter list. It
  244. s otherwise identical.)
  245. AEBuildAppleEvent
  246. AEBuildAppleEvent(
  247. )l'AEEventClass theClass, AEEventID theID,
  248. <DescType addressType, void *addressData, long addressLength,
  249. #short returnID, long transactionID,
  250. AppleEvent *event,
  251. !const char *descriptorStr, ... );
  252. AEBuildAppleEvent
  253. )f     is like 
  254. AEBuild
  255. )*% but builds an Apple event, including
  256. N<parameters and attributes. Or, you could say that it
  257. s like 
  258. AEBuildParameters
  259. creates the event from scratch.
  260. Palatino
  261. The Apple Event Builder/Printer
  262. 20 March 1995
  263. Page 
  264. f7Most of the parameters are just like the parameters to 
  265. Courier
  266. AECreateAppleEvent
  267. fNalthough you pass the target address data directly, instead of via a pre-built
  268. 9descriptor. The resulting Apple event will appear in the 
  269. event
  270.  parameter.
  271. descriptorStr
  272. )NA specifies the parameters (required and optional) and attributes.
  273. fPThe syntax is described below (see especially the Apple Event Descriptor Strings
  274. 4subsection); it
  275. s almost the same as the syntax for 
  276. AEBuild
  277. , with a few additions
  278. and modifications.
  279. vAEBuildAppleEvent
  280.  is analogous to 
  281. vprintf
  282. )*#: Instead of passing the parameters
  283. f&along with the function, you supply a 
  284. va_list
  285. , as defined in 
  286. <stdarg.h>
  287. , that
  288. f8points to the parameter list. It
  289. s otherwise identical.)
  290. AEPrint
  291. *":OSErr AEPrint( AEDesc *desc, char *bufStr, long bufSize );
  292. AEPrint
  293. )*" reads the Apple event descriptor 
  294.  and writes a corresponding
  295. f0descriptor string into the string pointed to by 
  296. bufStr
  297. . It will write no more than
  298. bufSize
  299. )*J characters, including the trailing null character. Any errors returned by
  300. fSApple Event Manager routines will be returned to the caller; this isn
  301. t very likely
  302. unless the 
  303. AEDesc
  304.  structure is somehow corrupt.
  305. f+The descriptor string produced, if sent to 
  306. AEBuild
  307. , will build a descriptor
  308. identical to the original one. 
  309. AEPrint
  310.  tries to detect 
  311. )M    AERecords
  312.  that have been
  313. fOcoerced to other types and print them as coerced records. Structures of unknown
  314. type that can
  315. t be coerced to 
  316.     AERecords
  317.  are dumped as hex data.
  318. AEPrint
  319. )*I can also print complete Apple events as well as regular descriptors. The
  320. fAsyntax of the resulting string for an event is like that used by 
  321. AEBuildParameters
  322. AEBuildAppleEvent
  323. , except that:
  324. GThe string begins with the event class and ID separated by a backslash.
  325. 1the parameter list is surrounded by curly braces.
  326. LAttributes are also displayed; they look like parameters but are preceded by
  327. Zapf Dingbats
  328. The builder functions do 
  329.   accept this event syntax yet.
  330. Palatino
  331. The Apple Event Builder/Printer
  332. Page 
  333. 20 March 1995
  334. AEPrintSize
  335. Courier
  336. *"7OSErr AEPrintSize( AEDesc *desc, long *bufSizeNeeded );
  337. AEPrintSize
  338.  computes the buffer size that 
  339. AEPrint
  340.  would require if given the
  341. NVsame descriptor. (The size is equal to the string length, plus 1 byte for the trailing
  342. 'null.) This is handy for pre-flighting 
  343. AEPrint
  344. )*$, if you want to allocate the buffer
  345. N6dynamically  instead of relying on one of fixed size .
  346. Palatino
  347. The Apple Event Builder/Printer
  348. 20 March 1995
  349. Page 
  350. Descriptor-String Syntax
  351. fSThe real meat of all this, of course, is the syntax of the descriptor strings. It
  352. Ipretty simple: basic data types like numbers and strings can be described
  353. Odirectly, and then built up into lists and records. I
  354. ve even provided a pseudo
  355. fLBNF grammar (next section) for those of you who actually enjoy reading those
  356. things.
  357. Basic Types
  358. The fundamental data types are:
  359. Examples
  360. )c    Type-code
  361. Description
  362. Integer
  363. Courier
  364. -5678
  365. 'long'
  366. 'shor'
  367. A sequence of decimal digits,
  368. optionally preceded by a minus
  369. sign.
  370. f    Enum/Type
  371. longint
  372. 'long'
  373. '8-)'
  374. 'enum'
  375. (Use coercion
  376. to change to
  377. 'type')
  378. ]!A magic four-letter code. Will be
  379. truncated or padded with spaces
  380. "to exactly four characters. If you
  381. put straight or curly single
  382. ] quotes around it, it can contain
  383.  any characters. If not, it can
  384. contain any of: 
  385. :-,([{}])
  386. and can
  387. t begin with a digit.
  388. String
  389. A String.
  390. Multiple lines
  391. are okay.
  392. 'TEXT'
  393. Any sequence of characters
  394. within open and close 
  395. curly
  396. ]!quotes. Won
  397. t be null-terminated.
  398. Hex Data
  399. 4170706C65
  400. 0102 03ff
  401.      e b 6 c
  402. (Must be
  403. coerced to
  404. some type)
  405. An even number of hex digits
  406. between French quotes (Option
  407. ]!\, Option-Shift-\). Whitespace is
  408. ignored.
  409. Palatino
  410. The Apple Event Builder/Printer
  411. Page 
  412. 20 March 1995
  413. Zapf Dingbats
  414. HYes, you have to use the actual four-letter codes for enums, type codes,
  415. Ikeywords and object types, instead of the mnemonic constants. Luckily the
  416. Fcodes are semi-mnemonic anyway. I did it this way to avoid the massive
  417. Koverhead, both in code size and execution speed, of a symbol table. You can
  418. Nfind the definitions of the constants in the text file 
  419. AEObjects.h
  420. , which is
  421. 2part of the Apple Events Object Support Library.
  422. Warning
  423. )D?Watch out for four-letter-codes that contain special characters
  424. Blike commas, parens, braces, or non-trailing spaces, or that begin
  425. with a special character like 
  426. Courier
  427.  . Put these in single quotes to
  428. avoid syntax errors.
  429. Coercion
  430. HAny basic element (except a hex string) by itself is a descriptor, whose
  431. LdescriptorType is as given in the table. You can coerce a basic element to a
  432. Sdifferent type by putting it in parentheses with a type-code placed before it. Here
  433. are some examples:
  434. sing(1234)
  435. type(line)
  436. long(CODE)
  437. hexd(
  438. A String
  439. 'blob'(
  440. 4170706C65
  441. 4Coercions of numeric values are effected by calling 
  442. AECoerceDesc
  443. ; if the
  444. coercion fails, you
  445. ll get an 
  446. errAECoercionFail
  447.  error returned to you.
  448. ^*Coercions of other types just replace the 
  449. descriptorType
  450.  field of the 
  451. AEDesc
  452. Hex strings 
  453. must 
  454. =be coerced to something, since they have no intrinsic type.
  455. NGYou can also coerce nothing, to get a descriptor with zero-length data:
  456. emty()
  457. Palatino
  458. The Apple Event Builder/Printer
  459. 20 March 1995
  460. Page 
  461. f+Even the type can be omitted, leaving just 
  462. Courier
  463. , in which case the type is '
  464. null'
  465. Lists
  466. To make an 
  467. AEDescList
  468. )<7, just enclose a comma-separated list of descriptors in
  469. square brackets. For example:
  470. [123, -456, 
  471. et cetera
  472. [sing(1234), long(CODE),
  473. wheels
  474. within wheels
  475. fWThe elements of a list can be of different types, and a list can contain other lists or
  476.  records (see below) as elements.
  477. ELists cannot be coerced to other types; the type of a list is always 
  478. 'list'
  479. Records
  480. AERecord
  481. )0E is indicated by a comma-separated list of elements enclosed in curly
  482. fQbraces. Each element of a record consists of a keyword (a type-code, as described
  483. "under Basic Types) followed by a 
  484. , followed by a value, which can be any
  485. f@descriptor: a basic type, a list or another record. For example:
  486. {x:100, y:-100}
  487. 3{'origin': {x:100, y:-100}, extent: {x:500, y:500},
  488.  cont: [1, 5, 25]}
  489. f The default type of a record is 
  490. 'reco'
  491. )$'. Many of the Apple Events Object Model
  492. structures are 
  493. )K    AERecords
  494. )65 that have been coerced to some other data type, like
  495. 'indx'
  496.  or '
  497. whos'
  498. ?. You can coerce a record structure to any type by preceding it
  499. with a type code. For example:
  500. rang{ star: 5, stop: 6}
  501. Palatino
  502. The Apple Event Builder/Printer
  503. Page 
  504. 20 March 1995
  505. Zapf Dingbats
  506. Warning
  507. )D&Coercing to an existing type, such as 
  508. Courier
  509. 'bool'
  510. 'TEXT'
  511. , is a bad
  512. /idea. Anyone parsing the descriptor (including 
  513. AEPrint
  514. ) will
  515. :recognize the type and assume that the data has the normal
  516. Cinterpretation, which in this case it wouldn
  517. t. Bad to awful things
  518. would happen. Don
  519. t do it.
  520. Apple Events
  521. KThe syntax of the formatting string for an entire Apple event (as passed to
  522. AEBuildAppleEvent
  523. )f=) is almost identical to that of a record. Each keyed element
  524. NJspecified in the string becomes a parameter or attribute of the event. The
  525. differences are:
  526. AThere are no curly-braces at the beginning and end of the string.
  527. ?The character 
  528.  before a parameter keyword makes it optional.
  529. Warning
  530. )D)The keyword for the direct parameter is 
  531. . Remember to
  532. ?put single-quotes around it, or the parser will see the first 
  533. and think it
  534. s found a number.
  535. NMHere
  536. s an example of how to construct an Open Selection event for the Finder:
  537. AliasHandle parent, itemToOpen;
  538. &const OSType finderSignature = 'MACS';
  539. AppleEvent event;
  540. OSErr err;
  541. )// Construct the aliases here (not shown)
  542. err= AEBuildEppleEvent(
  543. 'FNDR', 'SOPE',
  544. =typeApplSignature, @finderSignature, sizeof(finderSignature),
  545. )kAutoGenerateReturnID, kAnyTransactionID,
  546. &event,
  547. // Event to be created
  548. : alis(@@), fsel: [alis(@@)]
  549. // Format string
  550. parent,
  551. // param for 1st @@
  552. itemToOpen
  553. // param for 2nd @@
  554. Palatino
  555. The Apple Event Builder/Printer
  556. 20 March 1995
  557. Page 
  558. Substituting Parameters
  559. GTo plug your own values into the midst of a descriptor, use the magic 
  560. Courier
  561. character. You can use 
  562.  anywhere you can put a basic element like an integer.
  563. Each 
  564.  is replaced by a value taken from the parameter list sent to the 
  565. AEBuild
  566. fIfunction. The type of value created depends on the context in which the 
  567. f&used: in particular, how it
  568. s coerced.
  569. Type Coerced to:
  570. Type of fn parameter read:
  571.     Comments:
  572. No coercion
  573. AEDesc*
  574. ]    A plain 
  575.  will be replaced with a
  576. descriptor parameter.
  577. f    Numeric (
  578. short
  579. short
  580. float
  581. short double
  582. double
  583. Remember that THINK C
  584. double
  585. corresponds to type 
  586. 'exte'
  587. char*
  588. ]&Pointer to a null-terminated C string.
  589. Any other type
  590.  followed by 
  591. void*
  592. )9&Expects a length parameter followed by
  593. !a pointer to the descriptor data.
  594. Zapf Dingbats
  595.     Important
  596. Note particularly: that 
  597. # parameters must be null-terminated
  598. @strings, although the resulting descriptor data will not be null
  599. .terminated; and that the general case expects 
  600.  parameters: the
  601. s size and location. 
  602. f<In addition, you can substitute data from a handle by using 
  603.  signs. An 
  604. fLparameter will read a single handle from the parameter list and use the data
  605. ?pointed to by that handle as the value of the descriptor. The 
  606.  must be
  607. coerced so that 
  608. AEBuild
  609. )*5 will know what type to make the descriptor; however,
  610. fAthe type coerced to can be anything (the table above is ignored.)
  611. NThis mechanism is still a bit limited, and may well be improved in the future.
  612. Palatino
  613. The Apple Event Builder/Printer
  614. Page 
  615. 20 March 1995
  616. Descriptor-String Grammar
  617. NNSince no language, however small, can be taken seriously unless it comes fully
  618. Lequipped with a formidable-looking BNF grammar specification, I here present
  619. Hone. No attempt has been made to prevent Messrs. Backus and/or Naur from
  620. (rolling over in their respective graves.
  621. Character Classification:
  622. whitespace
  623. digit
  624. paren, bracket,
  625. braces
  626. single-quote
  627. double quotes
  628. hex quotes
  629. colon
  630. comma
  631. at-sign
  632. r    identchar
  633. any other printable character
  634. Tokens:
  635.     ident ::=
  636. )l    identchar
  637.     identchar
  638. digit
  639. Padded/truncated
  640.     character
  641. to exactly 4 chars
  642. integer ::=
  643. digit
  644. Just as in C
  645. string ::=
  646.     character
  647. hexstring ::=
  648. hexdigit
  649. whitespace
  650. Even no. of digits, please
  651. Grammar Rules for AEBuild:
  652. formatstring ::=
  653. This is the top level of syntax
  654. obj ::=
  655. Single AEDesc; shortcut for 
  656.     structure
  657. Un-coerced structure
  658. ident
  659.  structure
  660. Coerced to some other type
  661. structure ::=
  662.  data 
  663. Single AEDesc
  664.  objectlist 
  665. AEList type
  666. Palatino
  667. The Apple Event Builder/Printer
  668. 20 March 1995
  669. Page 
  670.  keywordlist 
  671. AERecord type
  672. objectlist ::=
  673. blank
  674. Comma-separated list of things
  675. obj [ 
  676.  obj ]
  677. keywordpair ::=
  678. ident
  679. Keyword/value pair
  680. keywordlist ::=
  681. blank
  682. List of said pairs
  683. keywordpair [ 
  684.  keywordpair ]
  685. data ::=
  686. Gets appropriate data from fn param
  687. integer
  688. 'shor' or 'long' unless coerced
  689. ident
  690. A 4-char type code ('type') unless coerced
  691. string
  692. Unterminated text; 'TEXT' type unless coerced
  693.     hexstring
  694. Raw hex data; 
  695.  be coerced to some type!
  696. f$Grammar Rules for AEBuildAppleEvent:
  697. eventstring ::=
  698. evtkeywordlist
  699. Top level syntax for AEBuildAppleEvent
  700. evtkeywordpair ::=
  701. ident
  702. Keyword/value pair
  703. evtkeywordlist ::=
  704. blank
  705. List of said pairs
  706. evtkeywordpair [ 
  707.  evtkeywordpair ]
  708. f)There. Now it
  709. s all crystal-clear, right?
  710. Palatino
  711. The Apple Event Builder/Printer
  712. Page 
  713. 20 March 1995
  714. An Example & Timing Comparison
  715. NRAs an example, I
  716. ll take a C function to generate an object descriptor (taken from
  717. La Pascal example in the Object Model ERS, fleshed out and with gobs of error
  718. +checking added) and turn it into a call to 
  719. Courier
  720. AEBuild
  721. . The object descriptor we want
  722. to generate is:
  723. >First line of document 'Spinnaker' whose first word is 'April'
  724. and whose second word is 'is'
  725. NCThen I
  726. ll execute both functions and compare their execution times.
  727. *4#C Code Using Object-Packing Library
  728. OSErr
  729. 6BuildByHand( AEDesc *dDocument, AEDesc *theResultObj )
  730. OSErr err;
  731. BAEDesc dObjectExamined, dNum, dWord1, dWord2, dAprilText, dIsText,
  732. O   dComparison1, dComparison2, dLogicalTerms, dTheTest, dLineOne, dTestedLines;
  733. dObjectExamined.dataHandle =
  734. 4/* Zero things to start out with so we can safely */
  735. dNum.dataHandle =
  736. 4/* execute our fail code if things don't work out */
  737. dWord1.dataHandle =
  738. dWord2.dataHandle =
  739. dAprilText.dataHandle =
  740. dIsText.dataHandle =
  741. dComparison1.dataHandle =
  742. dComparison2.dataHandle =
  743. dLogicalTerms.dataHandle =
  744. dTheTest.dataHandle =
  745. dLineOne.dataHandle =
  746. dTestedLines.dataHandle =
  747. r;if( err= AECreateDesc( 'exmn', NIL, 0, &dObjectExamined ) )
  748. goto fail;
  749. r'if( err= MakeIndexDescriptor(1,&dNum) )
  750. goto fail;
  751. Palatino
  752. The Apple Event Builder/Printer
  753. 20 March 1995
  754. Page 
  755. Courier
  756. Gif( err= MakeObjDescriptor( 'word', &dObjectExamined, formIndex, &dNum,
  757. false, &dWord1) )
  758. goto fail;
  759. :if( err= AECreateDesc( 'TEXT', "April", 5, &dAprilText ) )
  760. goto fail;
  761. AEDisposeDesc(&dNum);
  762. 'if( err= MakeIndexDescriptor(2,&dNum) )
  763. goto fail;
  764. Gif( err= MakeObjDescriptor( 'word', &dObjectExamined, formIndex, &dNum,
  765. true, &dWord2) )
  766. goto fail;
  767. 4if( err= AECreateDesc( 'TEXT', "is", 2, &dIsText ) )
  768. goto fail;
  769. Rif( err= MakeCompDescriptor( '=   ', &dAprilText, &dWord1, true, &dComparison1 ) )
  770. goto fail;
  771. .if( err= MakeCompDescriptor( '=   ', &dIsText,
  772. "  &dWord2, true, &dComparison2 ) )
  773. goto fail;
  774. 8if( err= AECreateList( NIL, 0, false, &dLogicalTerms ) )
  775. goto fail;
  776. 6if( err= AEPutDesc( dLogicalTerms, 1, dComparison1 ) )
  777. goto fail;
  778. 6if( err= AEPutDesc( dLogicalTerms, 2, dComparison2 ) )
  779. goto fail;
  780. AEDisposeDesc(&dComparison1);
  781. AEDisposeDesc(&dComparison2);
  782. Jif( err= MakeLogicalDescriptor( &dLogicalTerms, 'AND ', true, &dTheTest) )
  783. goto fail;
  784. Hif( err= MakeObjDescriptor(classLine,&dDocument,formTest,&dTheTest,true,
  785. &dTestedLines) )
  786. goto fail;
  787. +if( err= MakeIndexDescriptor(1,&dLineOne) )
  788. goto fail;
  789. Kif( err= MakeObjDescriptor( classLine, &dTestedLines, formIndex, &dLineOne,
  790. true, theResultObj ) )
  791. goto fail;
  792. return noErr;
  793. fail:
  794. +/* Clean up in case we couldn't build it */
  795. AEDisposeDesc(theResultObj);
  796.  AEDisposeDesc(&dObjectExamined);
  797. Palatino
  798. The Apple Event Builder/Printer
  799. Page 
  800. 20 March 1995
  801. Courier
  802. AEDisposeDesc(&dNum);
  803. AEDisposeDesc(&dWord1);
  804. AEDisposeDesc(&dWord2);
  805. AEDisposeDesc(&dAprilText);
  806. AEDisposeDesc(&dIsText);
  807. AEDisposeDesc(&dComparison1);
  808. AEDisposeDesc(&dComparison2);
  809. AEDisposeDesc(&dLogicalTerms);
  810. AEDisposeDesc(&dTheTest);
  811. AEDisposeDesc(&dLineOne);
  812. AEDisposeDesc(&dTestedLines);
  813. return err;
  814. *!8MPW 3.2b5 C compiled this into 816 bytes of object code.
  815. II found that the average time to execute this function was 0.0188 seconds
  816. &(Quadra 700) or 0.0113 seconds (IIfx).
  817. * Use this figure for comparison only; your
  818. NMtimes may vary. The timing is especially dependent on the number of blocks in
  819. Fthe heap, since so many block allocations and disposals are happening.
  820. Descriptor String
  821. obj{ want:type('line'),
  822. :     from: obj{ want: type('line'), from: @, form: 'test',
  823.                 seld: logi{
  824. =                           term: [comp{ relo:=, obj1:
  825. April
  826. -                                        obj2:
  827. X                              obj{ want:type('word'), from:exmn(), form:indx, seld:1 }},
  828. :                                  comp{ relo:=, obj1:
  829. -                                        obj2:
  830. W                              obj{ want:type('word'), from:exmn(), form:indx, seld:2 }}
  831. #                                 ],
  832. #                           logc:AND
  833.                          }
  834.               },
  835.      form: 'indx',
  836.      seld: 1
  837. \ Yes, it really took half again as long on a Quadra! I think that cache flushing during the 
  838. NOcall is responsible. (It barely slows down at all when you disable the caches.)
  839. Palatino
  840. The Apple Event Builder/Printer
  841. 20 March 1995
  842. Page 
  843. AEBuild Call
  844. Courier
  845. char descriptor[] =
  846. 2/* Same descriptor string as above. Note clever */
  847. "obj{ want:type('line'),"
  848. //* method used to break string across lines. */
  849. f;    "from: obj{ want: type('line'), from: @, form: 'test',"
  850. /* Note parameter here */
  851.                "seld: logi{"
  852. >                          "term: [comp{ relo:=, obj1:
  853. April
  854. .                                       "obj2:"
  855. Y                             "obj{ want:type('word'), from:exmn(), form:indx, seld:1 }},"
  856. ;                                 "comp{ relo:=, obj1:
  857. .                                       "obj2:"
  858. X                             "obj{ want:type('word'), from:exmn(), form:indx, seld:2 }}"
  859. $                                "],"
  860. $                          "logc:AND"
  861.                         "}"
  862.              "},"
  863.     "form: 'indx',"
  864.     "seld: 1"
  865. ,void PackWordDesc( AEDesc *dDocumentObject )
  866. Spinnaker
  867.  descriptor is a parameter */
  868. err = AEBuild(&theResultObj,
  869. descriptorString,
  870. dDocumentObject);
  871. /* AEDesc* parameter for "@" */
  872. *!NMPW 3.2b5 C compiled this into 42 bytes of object code, plus 310 bytes of data
  873. storage for the string.
  874. II found that the average time to execute this function was 0.0049 seconds
  875. P(Quadra 700) or 0.0070 seconds (IIfx). Use this figure for comparison only; your
  876. Ltimes may vary. The timing is dependent on the number of blocks in the heap,
  877. 2since heap blocks are being allocated and resized.
  878. Palatino
  879. The Apple Event Builder/Printer
  880. Page 
  881. 20 March 1995
  882. Timing Conclusions
  883. RWith previous versions of this library, there was a 70% increase in execution time
  884. when using the 
  885. Courier
  886. AEBuild
  887. )*1 routine. After delivering the bad news, I wrote:
  888. rEHowever, if speed does become an issue, there is always the option of
  889. turbocharging 
  890. AEBuild
  891. 5by having it directly build descriptors without going
  892. rOthrough the Apple Event Manager functions at all. This would save an incredible
  893. @number of Memory Manager calls and probably increase performance
  894. severalfold. Anyone using 
  895. AEBuild
  896. )will get all these improvements for free.
  897. NSThis is exactly what I did in version 1.1. In fact, I wrote a library (AEStream) to
  898. 'do it, so you can do it too. It
  899. s easy.
  900. AEBuild
  901. ,is now 1.5 to 4 times as fast (depending on 
  902. ) as the using the Apple
  903. N5Event Manager and/or Object Packing Library routines.
  904.  (This means that
  905. NKAEStream was responsible for a threefold speed-up in AEBuild. Not bad, when
  906. Eyou take into account other overhead like parsing the format string!)
  907. +Needless to say, if you were already using 
  908. AEBuild
  909.  you get this speed increase
  910. absolutely free. Enjoy!
  911. Palatino
  912. The Apple Event Builder/Printer
  913. 20 March 1995
  914. Page 
  915. The Demo Program
  916. ve included a demonstration program in the distribution. This is a program I
  917. Vused to debug the library. It reads a line of input, uses AEBuild to translate it into
  918. Nan AEDesc, uses AEPrint to translate the AEDesc back into a string, and prints
  919. Qeach resulting string. Error codes are reported, including syntax-error messages.
  920. QThe source code is provided in case you want to see how the functions are called.
  921. Zapf Dingbats
  922. Warning
  923. The demo tool does 
  924. % handle parameter substitution (the 
  925. Courier
  926. :character). If you try to substitute parameters, messy and
  927. =unpleasant things may happen. Use some numeric value in place
  928. )of parameters, and then replace it with 
  929. s after you paste the
  930. string into your program.
  931. E" S71*U
  932. +@A='*
  933. p 0p`P
  934. H9;BE
  935. "&)+-
  936. L,.$2
  937. ( @$$
  938. xP(H`X
  939.     G'()*,-./
  940. 1"!$?
  941. 9:48*
  942. B4+,-.653
  943.     temp.0001
  944. """"""?
  945. Jens Alfke
  946. Apple Computer
  947. Microsoft Word
  948. New York
  949. Zapf Dingbats
  950. Palatino
  951. Courier
  952. bPREC
  953. nPRVS
  954. zCAPN
  955.